<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">

    <body>

        <ui:composition template="./TemplatePrincipal.xhtml">

            <ui:define name="top">
                <h:form>
                    <p:tabMenu id="menu" activeIndex="3" >  
                        <p:menuitem value="Home" action="MenuPrincipal" />  
                        <p:menuitem value="Users" action="UsuarioFacelets"/>  
                        <p:menuitem value="Author" action="AuthorFacelets" />  
                        <p:menuitem value="Category" action="CategoryFacelets" />
                        <p:menuitem value="Book" action="BookFacelets" />
                    </p:tabMenu> 
                </h:form>
            </ui:define>

            <ui:define name="left">
                left
            </ui:define>

            <ui:define name="content">

                <f:view>
                    <h:form id="form" onreset="">
                        <p:panel id="as" visible="false">
                            <f:facet name="options">  
                                <p:menu>  
                                </p:menu>  
                            </f:facet> 
                        </p:panel>

                        <p:panel  id="pnl" header="New category" toggleable="true" toggleOrientation="horizontal" collapsed="true" 
                                  widgetVar="panel" style="text-align-last: justify">  
                            <p:growl id="growl" showDetail="true" /> 

                            <h1><p:outputLabel value="Create/Edit"/></h1>
                            <p:panelGrid id="grid" columns="2" style="text-align: right">
                                <p:outputLabel value="Clave:" for="clave" />
                                <p:inputText id="clave" value="#{categoryBean.category.clave}" title="Clave" required="true" requiredMessage="The Clave field is required."/>
                                <p:outputLabel value="Descripcion:" for="descripcion" />
                                <p:inputText id="descripcion" value="#{categoryBean.category.descripcion}" title="Descripcion" required="true" requiredMessage="The Descripcion field is required."/>
                                <p:outputLabel value="Color:" for="color" />
                                <p:inputText id="color" value="#{categoryBean.category.color}" title="Color" />

                                <p:commandButton id="button" value="Guardar" update="form:growl :form:pnl :form:displa"
                                                 oncomplete="if(!args.validationFailed){categoryDialog.show()}" />
                                <p:commandButton value="Cancelar" onclick="panel.toggle()" update="pnl" immediate="true" /> 
                            </p:panelGrid>


                            <p:ajax event="toggle" listener="#{categoryBean.handleToggle}" update=":form:growl :form:pnl :form:as"  process="@this"/>  


                        </p:panel>  
                        <p:dialog id="dia" header="Confirm Category" widgetVar="categoryDialog" resizable="false"  
                                  showEffect="fade" hideEffect="explode">  
                            <h:panelGrid id="displa" columns="2" cellpadding="4">  
                                <h:panelGrid id="r" columns="2" > 
                                    <p:outputLabel value="Clave:"/>
                                    <p:outputLabel value="#{categoryBean.category.clave}" title="Clave"/>
                                    <p:outputLabel value="Descripcion:"/>
                                    <p:outputLabel value="#{categoryBean.category.descripcion}" title="Descripcion"/>
                                    <p:outputLabel value="Color:"/>
                                    <p:outputLabel value="#{categoryBean.category.color}" title="Color"/>
                                    <p:commandButton value="Guardar"  action="#{categoryBean.crearCategoria()}" onclick="categoryDialog.hiden()" />
                                    <p:commandButton value="Cancelar" onclick="categoryDialog.hiden()" update="pnl" immediate="true" /> 
                                </h:panelGrid> 
                            </h:panelGrid>
                        </p:dialog> 
                    </h:form>
                </f:view>

                <f:view id="fg">
                    <h:form id="formCa">
                        <p:growl id="growl" showDetail="true"/>

                        <h1><h:outputText value="List"/></h1>
                        <p:dataTable value="#{categoryBean.list}" var="item" widgetVar="categoryTable" rowKey="#{item.id}"
                                      emptyMessage="No category found with given criteria"
                                     selection="#{categoryBean.category}" selectionMode="single">


                            <p:ajax event="rowSelect" listener="#{categoryBean.edit}" update=":formCa:display :formCa:growl" 
                                    oncomplete="cateDialog.show()" /> 
                            <p:ajax event="rowUnselect" listener="#{categoryBean.RowUnselect}" update=":formCa:growl"/>  

                            <f:facet name="header">
                                <p:outputPanel>
                                    <p:outputLabel value="Search all fields"/>
                                    <p:inputText id="globalFilter" onkeyup="categoryTable.filter()" style="width: 150px"/>
                                </p:outputPanel>
                            </f:facet>

                            <p:column filterBy="#{item.clave}">
                                <f:facet name="header">
                                    <h:outputText value="Clave"/>
                                </f:facet>
                                <h:outputText value="#{item.clave}"/>
                            </p:column>
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Descripcion"/>
                                </f:facet>
                                <h:outputText value="#{item.descripcion}"/>
                            </p:column>
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Color"/>
                                </f:facet>
                                <h:outputText value="#{item.color}"/>
                            </p:column>
                        </p:dataTable>

                        <p:dialog id="dialog" header="Car Detail" widgetVar="cateDialog" resizable="false"  
                                  showEffect="fade" hideEffect="explode">  

                            <h:panelGrid id="display" columns="2" cellpadding="4">  
                                <h:panelGrid id="d" columns="2">
                                    <h:outputLabel value="Clave:" for="clave" />
                                    <p:inplace id="ajaxInplac" editor="true">  
                                        <h:inputText id="clave" value="#{categoryBean.category.clave}" title="Clave" required="true" requiredMessage="The Clave field is required."/>
                                    </p:inplace>
                                    <h:outputLabel value="Descripcion:" for="descripcion" />
                                    <p:inplace id="ajaxInpla" editor="true">  
                                        <h:inputText id="descripcion" value="#{categoryBean.category.descripcion}" title="Descripcion" required="true" requiredMessage="The Descripcion field is required."/>
                                    </p:inplace>
                                    <h:outputLabel value="Color:" for="color" />
                                    <p:inplace id="ajaxInp" editor="true">  
                                        <h:inputText id="color" value="#{categoryBean.category.color}" title="Color" />
                                    </p:inplace>
                                    <p:commandButton update="@(form)" value="GuardarCambios"  action="#{categoryBean.editCategory()}" />
                                </h:panelGrid>
                            </h:panelGrid>  

                        </p:dialog> 
                    </h:form>
                </f:view>

            </ui:define>

        </ui:composition>

    </body>
</html>
